/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controlador;

import Facade.VoluntarioFacade;
import VO.VoluntarioVO;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.actions.DispatchAction;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import vista.voluntarioBean;

/**
 *
 * @author Alan
 */
public class ModificarVoluntarioAction extends DispatchAction {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    private final static String FAILURE = "failure";
    
    /**
     * This is the Struts action method called on
     * http://.../actionPath?method=cargar,
     * where "method" is the value specified in <action> element : 
     * ( <action parameter="method" .../> )
     */
    public ActionForward Cargar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        VoluntarioFacade fcdVoluntario = new VoluntarioFacade();
        Vector lista = fcdVoluntario.cargarVoluntarios();
        request.setAttribute("listaVoluntarios", lista);

        VoluntarioVO datos = new VoluntarioVO();
        voluntarioBean frm = (voluntarioBean)form;

        datos = fcdVoluntario.cargarVoluntario(frm.getRut());
        request.setAttribute("voluntario", datos);

        if(datos != null) return mapping.findForward(SUCCESS);
        else return mapping.findForward(FAILURE);
    }


    public ActionForward Modificar(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        VoluntarioFacade fcdVoluntario = new VoluntarioFacade();
        voluntarioBean frm = (voluntarioBean)form;
        VoluntarioVO voluntarioMod = new VoluntarioVO();

        if((frm.getApellidos().length()<1)|| frm.getCargo().isEmpty()|| frm.getDireccion().isEmpty()
            ||frm.getFechaIngreso().isEmpty()||frm.getFechaNacimiento().isEmpty()
            ||frm.getGrupoSanguineo().isEmpty()||frm.getNombres().isEmpty()||frm.getRut().isEmpty())
        {
            String aviso = "<span style='color:red;font-weight:bold'>Debe ingresar todos los campos</span>";
                    request.setAttribute("aviso",aviso);
                     request.setAttribute("voluntario", frm);
                    return mapping.findForward(FAILURE);
        }

        voluntarioMod.setIdVoluntario(frm.getIdVoluntario());
        voluntarioMod.setRut(frm.getRut());
        voluntarioMod.setNombres(frm.getNombres());
        voluntarioMod.setApellidos(frm.getApellidos());
        voluntarioMod.setDireccion(frm.getDireccion());
        voluntarioMod.setEmail(frm.getEmail());
        voluntarioMod.setGrupoSanguineo(frm.getGrupoSanguineo());
        voluntarioMod.setCargo(frm.getCargo());
        voluntarioMod.setFechaNacimiento(frm.getFechaNacimiento());
        voluntarioMod.setFechaIngreso(frm.getFechaIngreso());
        if(frm.getFechaRetiro().isEmpty())
        {
            voluntarioMod.setFechaRetiro(null);
        }
        else voluntarioMod.setFechaRetiro(frm.getFechaRetiro());

        boolean resultado = fcdVoluntario.modificarVoluntario(voluntarioMod);

        Vector lista = fcdVoluntario.cargarVoluntarios();
        request.setAttribute("listaVoluntarios", lista);

        if(resultado) 
        {
            String aviso = "<span style='color:green;font-weight:bold'>Voluntario actualizado Exitosamente</span>";
            request.setAttribute("voluntario", frm);
            request.setAttribute("aviso",aviso);
            return mapping.findForward(SUCCESS);

        }
        else return mapping.findForward(FAILURE);
    }
}